package com.drinks.yiniang.io.api;

import com.alibaba.fastjson.JSON;
import com.drinks.yiniang.entity.comment.CommentJson;
import com.drinks.yiniang.entity.comment.CommentSubmitJson;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.user.wine.WineListDetailJson;
import com.drinks.yiniang.entity.user.wine.WineListJson;
import com.drinks.yiniang.io.http.HttpUrls;
import com.drinks.yiniang.utils.SharedPreUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.utils.LogUtils;
import com.hansen.library.utils.StringUtils;

/**
 * 酒单api
 * Created by han on 2019/3/6 0006.
 */

public class WineListApiIO extends BaseApiIO {
    volatile static WineListApiIO instance;

    static public WineListApiIO getInstance() {
        if (instance == null) {
            synchronized (WineListApiIO.class) {
                if (instance == null) {
                    instance = new WineListApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 酒单列表
     *
     * @param callback
     */
    public void getWineList(final APIRequestCallback<WineListJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();

        MainApiIO.getInstance().postRequest(HttpUrls.API_WINE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("酒单列表===onSuccess");

                WineListJson mDataJson = JSON.parseObject(res, WineListJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 创建酒单
     *
     * @param title     标题
     * @param mark      描述
     * @param isPublic  是否公开 1公开 0私密 默认1
     * @param isDefault 是否默认 1是 0否 默认 0
     * @param callback
     */
    public void createWineList(String title, String mark, String isPublic, String isDefault,
                               final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("title", title);
        if (!StringUtils.isEmpty(mark)) {
            paramsMap.put("mark", mark);
        }
        paramsMap.put("is_public", isPublic);
        //paramsMap.put("is_default", isDefault); //后台默认一个默认酒单，前端接口不用传了

        MainApiIO.getInstance().postRequest(HttpUrls.API_CREATE_WINE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("创建酒单===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 加入酒单
     *
     * @param wineListId 酒单id
     * @param goodsId    商品id
     * @param joinReason 加入描述
     * @param callback
     */
    public void joinWineList(String wineListId, String goodsId, String joinReason, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", wineListId);
        paramsMap.put("goods_id", goodsId);

        if (!StringUtils.isEmpty(joinReason)) {
            paramsMap.put("join_reason", joinReason);
        }

        MainApiIO.getInstance().postRequest(HttpUrls.API_ADD_TO_WINE_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("加入酒单===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 酒单详情
     *
     * @param wineListId 酒单id
     * @param callback
     */
    public void getWineListDetail(String wineListId, final APIRequestCallback<WineListDetailJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", wineListId);
        paramsMap.put("current_fans_id", SharedPreUtils.getInstance().getUserID());

        MainApiIO.getInstance().postRequest(HttpUrls.API_WINE_LIST_DETAIL, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("酒单详情===onSuccess");

                WineListDetailJson mDataJson = JSON.parseObject(res, WineListDetailJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 酒单商品加入购物车
     *
     * @param wineListId 酒单id
     * @param callback
     */
    public void addWineListGoodsToCart(String wineListId, final APIRequestCallback<StringJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", wineListId);

        MainApiIO.getInstance().postRequest(HttpUrls.API_ADD_WINE_LIST_GOODS_TO_SHOPPING_CART, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("酒单商品加入购物车===onSuccess");

                StringJson mDataJson = JSON.parseObject(res, StringJson.class);

                if (callback != null) {
                    if (mDataJson != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    //--------------------------------------评论

    /**
     * 获取酒单评论列表
     *
     * @param wineListId 酒单id
     * @param page
     * @param callback
     */
    public void getWineListComments(String wineListId, int page, final APIRequestCallback<CommentJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", wineListId);
        paramsMap.put("current_fans_id", SharedPreUtils.getInstance().getUserID());
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("per_page", Constants.PAGE_SIZE);

        MainApiIO.getInstance().postRequest(HttpUrls.API_WINE_LIST_COMMENTS_LIST, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("获取酒单评论列表===onSuccess");

                CommentJson mDataJson = JSON.parseObject(res, CommentJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }

    /**
     * 提交酒单评论
     *
     * @param wineListId 酒单id
     * @param content    评价内容
     * @param pid        回复的父级id
     * @param callback
     */
    public void doWineListComments(String wineListId, String content, String pid, final APIRequestCallback<CommentSubmitJson, Tuple2<Integer, String>> callback) {
        paramsMap.clear();
        paramsMap.put("wine_bills_id", wineListId);
        paramsMap.put("content", content);
        paramsMap.put("pid", pid);

        MainApiIO.getInstance().postRequest(HttpUrls.API_PUBLISH_WINE_LIST_COMMENT, paramsMap, new APIRequestCallback<String, Tuple2<Integer, String>>() {

            @Override
            public void onSuccess(String res) {
                LogUtils.d("提交酒单评论===onSuccess");

                CommentSubmitJson mDataJson = JSON.parseObject(res, CommentSubmitJson.class);

                if (callback != null) {
                    if (mDataJson != null && mDataJson.getData() != null) {
                        callback.onSuccess(mDataJson);
                    } else {
                        callback.onError(new Tuple2<>(RESULT_ERROR_CODE, RESULT_ERROR_MSG));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onComplete() {
                if (callback != null) {
                    callback.onComplete();
                }
            }
        });
    }
}
